//
//  VCOfDetails.m
//  17-HomeWork(通讯录)
//
//  Created by ParkourH on 16/8/6.
//  Copyright © 2016年 ParkourH. All rights reserved.
//

#import "VCOfDetails.h"

@interface VCOfDetails () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@end

@implementation VCOfDetails

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveContactInfo)];

    self.nameOfContact.text = _contact.name;
    self.ageOfContact.text = [NSString stringWithFormat:@"%ld", _contact.age];
    self.sexOfContact.text = _contact.gender;
    self.phoneNumberOfContact.text = [NSString stringWithFormat:@"%ld", _contact.phoneNumber];
//    self.phoneNumberOfContact.text = _contact.phoneNumber;
    self.imageOfContact.clipsToBounds = YES;
    self.imageOfContact.layer.cornerRadius = self.view.bounds.size.width / 12;
    
    
    [self pickImage];
}


- (void)setContact:(Contact *)contact {

    _contact = contact;
   
}

- (void)saveContactInfo {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.imageOfContact.image forKey:@"image"];
    [dic setValue:self.nameOfContact.text forKey:@"name"];
    [dic setValue: self.phoneNumberOfContact.text forKey:@"phoneNumber"];
    
    Contact *contact = [[Contact alloc] init];
    
    [contact setValuesForKeysWithDictionary:dic];
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(save:)]) {
        [self.delegate save:contact];
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:@"DLS160710(1)" ofType:@"plist"];
    NSLog(@"%@", path);
    
}


/** 选择照片 */
- (void)pickImage {
    
    self.imageOfContact.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pickPhoto)];
    [self.imageOfContact addGestureRecognizer:tap];
}

- (void)pickPhoto {
    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
    
    self.imageOfContact.image = image;
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
